package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerSecondhandObtainedComponent;
import com.kmproject.haifu.mvp.contract.SecondhandObtainedContract;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoreMainBean;
import com.kmproject.haifu.mvp.model.entity.beans.ObtainedSecondHandBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ObtainParmartBean;
import com.kmproject.haifu.mvp.presenter.SecondhandObtainedPresenter;
import com.kmproject.haifu.mvp.ui.adapter.SenondHandObtainedAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/03/2019 22:19
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class SecondhandObtainedActivity extends BaseActivity<SecondhandObtainedPresenter> implements SecondhandObtainedContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.secondHand_title_lay)
    LinearLayout secondHandTitleLay;
    @BindView(R.id.store_secondHand_RecyclerView)
    RecyclerView storeSecondHandRecyclerView;
    @BindView(R.id.secondHand_refreshLayout)
    SmartRefreshLayout secondHandRefreshLayout;
    MyStoreMainBean myStoreMainBean;
    SenondHandObtainedAdapter senondHandObtainedAdapter;
    ObtainParmartBean obtainParmartBean;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSecondhandObtainedComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_secondhand_obtained; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        obtainParmartBean.setLimit(AppConstant.pageSize_big);
          storeSecondHandRecyclerView.post(new Runnable() {
              @Override
              public void run() {
                  toolbarTitle.setTextColor(ArmsUtils.getColor(SecondhandObtainedActivity.this, R.color.white));
                  toolbar.setBackgroundColor(ArmsUtils.getColor(SecondhandObtainedActivity.this, R.color.color_3F3F4B));
                  toolbarBackImg.setImageResource(R.drawable.member_center_main_back_img);
                  backRightTxt.setVisibility(View.INVISIBLE);
              }
          });
        secondHandRefreshLayout.setEnableRefresh(false);
        senondHandObtainedAdapter=new SenondHandObtainedAdapter(new ArrayList<>(), new SenondHandObtainedAdapter.ItemClickcallBack() {
            @Override
            public void callBack(SecondHandInfoBean secondHandInfoBean) {
                new AlertView("提示", "确定删除此内容？", "取消", new String[]{"删除"}, null, SecondhandObtainedActivity.this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == AlertView.CANCELPOSITION) {
                            //点击取消按钮

                        }else {
                            mPresenter.remoredSencond(secondHandInfoBean.getSecondHandId());
                        }
                    }
                }).show();

            }
        });
        secondHandRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                obtainParmartBean.setPage(obtainParmartBean.getPage()+1);
           mPresenter.getLowerShelfList(obtainParmartBean);
            }
        });
        storeSecondHandRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        storeSecondHandRecyclerView.setAdapter(senondHandObtainedAdapter);

        if (getIntent().hasExtra("MyStoreMainBean"))
        {
            myStoreMainBean=(MyStoreMainBean)getIntent().getSerializableExtra("MyStoreMainBean");
        }
         obtainParmartBean=new ObtainParmartBean();
        if (getIntent().hasExtra("type")&&getIntent().getStringExtra("type").equals("userObtained"))
        {
            //从我的发布进来的，查看当前用户所有的下架信息
            obtainParmartBean.setPublishWay(0);
            UserBean userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);//获取用户信息
            obtainParmartBean.setUserId(Long.parseLong(userBean.getUserId()));
            secondHandTitleLay.setVisibility(View.GONE);
        }else {
            obtainParmartBean.setPublishWay(1);
            obtainParmartBean.setUserStoreId(Long.parseLong(myStoreMainBean.getStoreId()));
            secondHandTitleLay.setVisibility(View.VISIBLE);
        }
        mPresenter.getLowerShelfList(obtainParmartBean);
    }

    @Override
    public String reSetTitle() {
        return "已下架商品";
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void remoredSencondSuccess(String secondHandId) {
        storeSecondHandRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                showMessage("删除成功");
            }
        });
        obtainParmartBean.setPage(1);
        mPresenter.getLowerShelfList(obtainParmartBean);
    }

    @Override
    public void getLowerShelfListSuccess(int curryPage, int totalPage, List<ObtainedSecondHandBean> secondHandInfoBeans) {
        secondHandRefreshLayout.finishLoadMore();
        if (secondHandInfoBeans == null || secondHandInfoBeans.isEmpty()) {
            secondHandRefreshLayout.setEnableLoadMore(false);
            if (curryPage == 1) {
                senondHandObtainedAdapter.setEmptyView(AppUtil.setEmptyView(SecondhandObtainedActivity.this, "没有搜索到相关信息，换个条件试试", "", null, true));
            }
            return;
        }
        obtainParmartBean.setPage(curryPage);
        if (curryPage >= totalPage) {
            secondHandRefreshLayout.setEnableLoadMore(false);
        } else {
            secondHandRefreshLayout.setEnableLoadMore(true);
        }
        if (senondHandObtainedAdapter.getData() == null || senondHandObtainedAdapter.getData().isEmpty()) {
            senondHandObtainedAdapter.setNewData(secondHandInfoBeans);
        } else {
            senondHandObtainedAdapter.addData(secondHandInfoBeans);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
